package com.magicode.portal.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsPersonRela;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.entiy.base.ProgramPersonRela;
import com.magicode.cms.entiy.base.TvFilmPerson;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.portal.service.ISecTvFilmPersonService;

@Service("secTvFilmPersonService")
@Transactional
public class SecTvFilmPersonServiceImpl implements ISecTvFilmPersonService{

	@Override
	public TvFilmPerson get(String id) {
		String hql = " from com.magicode.cms.entiy.base.TvFilmPerson t "
				+ " left join fetch t.mainPic "
				+ " left join fetch t.sex"
				+ " left join fetch t.country"
//				+ " left outer join fetch t.tags"
				+ " left outer join fetch t.types "
//				+ " left outer join fetch t.programList "
//				+ " left outer join fetch t.relaPics "
				+ " where t.id=? ";
		TvFilmPerson tvFilmPerson = null;
		try {
			tvFilmPerson = (TvFilmPerson) this.commonDaoService.getByHQL(hql, new String[]{id});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tvFilmPerson;
	}
	
	@Override
	public List<Program> getProgramList(String id) {
		List<Program> programList = new ArrayList();
		String sql = " select p from "+ProgramPersonRela.class.getName()+" ppr left join ppr.person t , "
				+Program.class.getName()+" p left outer join fetch p.mainPic "
				+ " where t.id = :id and ppr.program = p and p.status = 2 and p.valid = 1 order by p.firstDateStr desc , ppr.orderCode asc ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try{
			programList = this.commonDaoService.queryList(sql, map);
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return programList;
	}
	
	@Override
	public List<Article> getArticles(String id) {
		List<Article> artList = new ArrayList();
		String hql = " select n from "+NewsPersonRela.class.getName()+" np  left join np.person p , "+Article.class.getName()+" n " 
				+ " where p.id = :id and np.news = n and n.status = 2 and n.valid = 1 order by n.subcribeTime desc , np.orderCode asc  ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			artList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return artList;
	}
	
	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;

}
